#include "app.h"
#include <string.h>


#define cUartBaudRate		19200
#define cI2CSpeedRate		100000

INT8U ubFlashVerifyRt = 0;

void sInitGpio(void);
void sInitAdc(void);


void sUart1Init(void);
void sUart2Init(void);
void sUart3Init(void);
void sUart4Init(void);
void sUart5Init(void);
void sGetCurrRole(void);
void sSaveRole(void);



// 硬件初始化
void sInitialMCU(void)
{
	OS_ENTER_CRITICAL();
	// 系统 时钟，上电SystemInit已经完成	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); // 抢断中断、子中断分级，这里不抢断
		
	
	// 嘀嗒系统时钟，设置最低优先级3 [0/1/2/3]	
	if (SysTick_Config(SystemCoreClock / 1000)) // 1ms
	{ 
		while (1);
	}
	
//	PendSV中断优先级，设置最低优先级3 [0/1/2/3]
	NVIC_SetPriority (PendSV_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL);

	sInitGpio();
	sInitAdc();
//	sUart1Init(); // PA9  PA10 RXD  TXD
	sUart2Init(); // PA2  PA3  RXD  TXD
	sUart3Init(); // PB10 PB11 RXD2 TXD2
	sUart4Init(); // PC10 PC11 RXD3 TXD3
	sUart5Init(); // PD2  PC12 RXD4 TXD4
	
}


void sInitGpio(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure;
	
	// -------- GPIOA --------
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 使能GPIOA时钟
	// Input
	GPIO_StructInit(&GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin  = JK_LP; // 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 浮空输入
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	// -------- GPIOB --------
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // 
	GPIO_StructInit(&GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin  = I_OUT | AJ_C | AJ_W | JK_UP; // 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 浮空输入
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	// Output
	GPIO_StructInit(&GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin  = OUT_H;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	// -------- GPIOC --------
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); // 使能GPIOA时钟
	// Input
	GPIO_StructInit(&GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin  = (JK_DN | AJ_RESET | JK_BU | JK_BD); // 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 浮空输入
	GPIO_Init(GPIOC, &GPIO_InitStructure);
	
	// Output
	GPIO_StructInit(&GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin  = (OUT_W | OUT_C | OUT_F);
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(GPIOC, &GPIO_InitStructure);
		
}




void ADC1PinConfig(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;

	// 1. 开启时钟（GPIOA/GPIOB/GPIOC在APB2）
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
   
	// 2. 配置模拟输入
	GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin = AD_TEMP;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;  // 模拟输入
    GPIO_Init(GPIOA, &GPIO_InitStruct);

	GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin = (AD_WATER | AD_CUP | AD_LAMP);
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;  // 模拟输入
    GPIO_Init(GPIOC, &GPIO_InitStruct);
}


void DMA_Config(void)
{
	DMA_InitTypeDef DMA_InitStruct;

	 // 1. 开启时钟（DMA1）
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

	// 2. 配置DMA（DMA1通道1用于ADC1）
    DMA_DeInit(DMA1_Channel1);
    DMA_InitStruct.DMA_PeripheralBaseAddr = (INT32U)&ADC1->DR; // ADC数据寄存器地址
    DMA_InitStruct.DMA_MemoryBaseAddr = (INT32U)ADC_ConvertedValue; // 内存目标地址
    DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;  // 外设为源
    DMA_InitStruct.DMA_BufferSize = ADC_CHANNEL_NUM; // 传输数据量
    DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // 外设地址固定
    DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable; // 内存地址自增
    DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; // 16位
    DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; // 16位
    DMA_InitStruct.DMA_Mode = DMA_Mode_Circular; // 循环模式
    DMA_InitStruct.DMA_Priority = DMA_Priority_High;
    DMA_InitStruct.DMA_M2M = DMA_M2M_Disable; // 非内存到内存
    DMA_Init(DMA1_Channel1, &DMA_InitStruct);
    DMA_Cmd(DMA1_Channel1, ENABLE);
	
}


void sADC1BasicConfig(void)
{
	ADC_InitTypeDef ADC_InitStruct;

	// 1. 开启时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

	// 2. 配置ADC时钟分频（6分频，64MHz / 6 ≈ 10.67MHz）
    RCC_ADCCLKConfig(RCC_PCLK2_Div6);  // 可选：RCC_PCLK2_Div2/4/6/8

	// 4. 配置ADC1（独立模式，扫描+连续转换）
	ADC_DeInit(ADC1);
    ADC_InitStruct.ADC_Mode = ADC_Mode_Independent; // 独立模式
    ADC_InitStruct.ADC_ScanConvMode = ENABLE;       // 多通道扫描
    ADC_InitStruct.ADC_ContinuousConvMode = ENABLE; // 连续转换
    ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; // 软件触发
    ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; // 数据右对齐
    ADC_InitStruct.ADC_NbrOfChannel = ADC_CHANNEL_NUM;  // 通道数
    ADC_Init(ADC1, &ADC_InitStruct);

    // 5. 配置ADC通道的采样时间
    ADC_RegularChannelConfig(ADC1, AD_TEMP_ch,  1, ADC_SampleTime_55Cycles5); //
    ADC_RegularChannelConfig(ADC1, AD_WATER_ch, 2, ADC_SampleTime_55Cycles5); //
    ADC_RegularChannelConfig(ADC1, AD_CUP_ch,   3, ADC_SampleTime_55Cycles5); //
    ADC_RegularChannelConfig(ADC1, AD_LAMP_ch,  4, ADC_SampleTime_55Cycles5); //
    
    // 6. 启用ADC DMA
    ADC_DMACmd(ADC1, ENABLE);

    // 7. 校准并启动ADC
    ADC_Cmd(ADC1, ENABLE);
    ADC_ResetCalibration(ADC1);
    while (ADC_GetResetCalibrationStatus(ADC1));
    ADC_StartCalibration(ADC1);
    while (ADC_GetCalibrationStatus(ADC1));
    ADC_SoftwareStartConvCmd(ADC1, ENABLE); // 开始转换
    
}

void sInitAdc(void)
{
	ADC1PinConfig();		// ADC引脚定向
	DMA_Config();
	sADC1BasicConfig();
}


void sUart1Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = cUartBaudRate;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART1, &USART_InitStructure);
	
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 使能接收中断
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 15; // 最低优先级
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Cmd(USART1, ENABLE);
}

void sUart2Init(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;			// PA2 USART2_TX
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;			// PA3 USART2_RX
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = cUartBaudRate;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART2, &USART_InitStructure);
	
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // 使能接收中断
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 15; // 最低优先级
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Cmd(USART2, ENABLE);
}

void sUart3Init(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;			// PB10 USART3_TX
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;			// PB11 USART3_RX
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = cUartBaudRate;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART3, &USART_InitStructure);
	
	USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); // 使能接收中断
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 15; // 最低优先级
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Cmd(USART3, ENABLE);
}


void sUart4Init(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;			// PC10/UART4_TX
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;			// PC11/UART4_RX
	GPIO_Init(GPIOC, &GPIO_InitStructure);
	
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = cUartBaudRate;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(UART4, &USART_InitStructure);
	
	USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); // 使能接收中断
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 15; // 最低优先级
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Cmd(UART4, ENABLE);
}


void sUart5Init(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;			// PC12/UART5_TX
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;			// PD2/UART5_RX
	GPIO_Init(GPIOD, &GPIO_InitStructure);
	
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = cUartBaudRate;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(UART5, &USART_InitStructure);
	
	USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); // 使能接收中断
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 15; // 最低优先级
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Cmd(UART5, ENABLE);
}


/******************************************************************************/
//Function name : sReadSaveData
//Description   : 
//                读取数据到MemPlace
//Parameters    : None
//Return        : None
/******************************************************************************/
void sReadSaveData(void) 
{
	uint16_t data_length = sizeof(Doctor) / sizeof(uint32_t);
	Flash_Read(FLASH_START_ADDR, (uint32_t *)&Doctor, data_length);
}

void sDataVerify(struct MEM_PLACE *doc)
{
	INT8U ubDataRt = 0;
	
	// P1 
	if(doc->P1.P_Seat > cPosSeatMax || doc->P1.P_Seat < cPosSeatMin)
	{
		ubDataRt = 1;
	}

	if(doc->P1.P_StBk > cPosStBkMax || doc->P1.P_StBk < cPosStBkMin)
	{
		ubDataRt = 1;
	}
	
	// P2
	if(doc->P2.P_Seat > cPosSeatMax || doc->P2.P_Seat < cPosSeatMin)
	{
		ubDataRt = 1;
	}

	if(doc->P2.P_StBk > cPosStBkMax || doc->P2.P_StBk < cPosStBkMin)
	{
		ubDataRt = 1;
	}

	// P3
	if(doc->P3.P_Seat > cPosSeatMax || doc->P3.P_Seat < cPosSeatMin)
	{
		ubDataRt = 1;
	}

	if(doc->P3.P_StBk > cPosStBkMax || doc->P3.P_StBk < cPosStBkMin)
	{
		ubDataRt = 1;
	}

	// 高极限位置
	if(doc->UpLmt.P_Seat > cPosSeatMax || doc->UpLmt.P_Seat < cPosSeatMin)
	{
		ubDataRt = 1;
	}

	if(doc->UpLmt.P_StBk > cPosStBkMax || doc->UpLmt.P_StBk < cPosStBkMin)
	{
		ubDataRt = 1;
	}

	// 低极限位置
	if(doc->DnLmt.P_Seat > cPosSeatMax || doc->DnLmt.P_Seat < cPosSeatMin)
	{
		ubDataRt = 1;
	}

	if(doc->DnLmt.P_StBk > cPosStBkMax || doc->DnLmt.P_StBk < cPosStBkMin)
	{
		ubDataRt = 1;
	}

	// 极限位置逻辑判断
	if(doc->DnLmt.P_Seat > doc->UpLmt.P_Seat)
	{
		ubDataRt = 1;
	}

	if(doc->DnLmt.P_StBk > doc->UpLmt.P_StBk)
	{
		ubDataRt = 1;
	}

	if(doc->CupTime > cCupTimeLmt)
	{
		ubDataRt = 1;
	}

	if(ubDataRt == 1)
	{
		ubFlashVerifyRt = 1;
		
		doc->P1.P_Seat = cPosSeatDefault;
		doc->P1.P_StBk = cPosStBkDefault;
		
		doc->P2.P_Seat = cPosSeatDefault;
		doc->P2.P_StBk = cPosStBkDefault;
		
		doc->P3.P_Seat = cPosSeatDefault;
		doc->P3.P_StBk = cPosStBkDefault;

		
		doc->UpLmt.P_Seat = cPosSeatUpLmt;
		doc->UpLmt.P_StBk = cPosStBkUpLmt;

		doc->DnLmt.P_Seat = cPosSeatDnLmt;
		doc->DnLmt.P_StBk = cPosStBkDnLmt;

		doc->CupTime    = cCupTimeDefault;
		doc->DisinfTime = cDisinfTimeDefault;
		doc->WashCase   = cWaterTimeDefault;
	}

}


/******************************************************************************/
//Function name : sSaveDataVerify
//Description   : 
//                从Flash读取数据校验
//Parameters    : None
//Return        : None
/******************************************************************************/
void sSaveDataVerify(void) 
{
	uint16_t data_length = sizeof(Doctor) / sizeof(uint32_t);

	ubFlashVerifyRt = 0;

	if(Doctor.Role > cDoctorC)
	{
		Doctor.Role = cDoctorA;
		ubFlashVerifyRt = 1;
	}
	sDataVerify(&Doctor.DocA);
	sDataVerify(&Doctor.DocB);
	sDataVerify(&Doctor.DocC);
	
	if(ubFlashVerifyRt == 1)
	{
		Flash_Write(FLASH_START_ADDR, (uint32_t *)&Doctor, data_length);
	}

	sGetCurrRole();
}



void sGetCurrRole(void)
{
	if(Doctor.Role == cDoctorB)
	{
		memcpy(&MemPlace, &Doctor.DocB, sizeof(MemPlace));
		mDocALedOff();
		mDocBLedOn();
		mDocCLedOff();
	}
	else if(Doctor.Role == cDoctorC)
	{
		memcpy(&MemPlace, &Doctor.DocC, sizeof(MemPlace));
		mDocALedOff();
		mDocBLedOff();
		mDocCLedOn();
	}
	else
	{
		memcpy(&MemPlace, &Doctor.DocA, sizeof(MemPlace));
		mDocALedOn();
		mDocBLedOff();
		mDocCLedOff();
	}
}



void sSaveRole(void)
{
	if(Doctor.Role == cDoctorB)
	{
		memcpy(&Doctor.DocB, &MemPlace, sizeof(MemPlace));
	}
	else if(Doctor.Role == cDoctorC)
	{
		memcpy(&Doctor.DocC, &MemPlace, sizeof(MemPlace));
	}
	else
	{
		memcpy(&Doctor.DocA, &MemPlace, sizeof(MemPlace));
	}
}

